package Questions;

/**
 * @version 3 11 February 2013
 * @author Myhaylo Kotsyuruba
 */

public class OneCorrectAnswerQuestions extends ManyAnswersQuestions {
    
    public OneCorrectAnswerQuestions(String text, int score, Answer[] answers) {
        super(text, score, answers);
    }

    public Question clone() {

        return new OneCorrectAnswerQuestions(getText(), getScore(),
                getAnswers());
    }

    public int rateUserAnswer(String userAnswer){
        int numberAnswer = Integer.parseInt(userAnswer);
        for (int index = 0; index < getQuantityAnswers(); index++) {
            if (isCorrectAnswer(index)) {
                if ((index + 1) == numberAnswer){
                    return getScore();
                }
            }
        }
        return 0;
    }

    @Override
    public String toString() {
        return String.format(
                "%s%nPlease, enter number of correct answer.",
                super.toString());
    }

    public static Question createQuestion(String data) {
        String[] fields = data.split(FIELDS_SEPARATOR);
        Answer[] answers = new Answer[(fields.length - 2) / 2];
        for (int index = 0; index < answers.length; index++) {
            answers[index] = new Answer(fields[index * 2 + 2],
                    Boolean.parseBoolean(fields[index * 2 + 3]));
        }
        return new OneCorrectAnswerQuestions(fields[0],
                Integer.parseInt(fields[1]), answers);
    }
}
